/**
 * 在单页模式下，为页面生成入口文件
 */

import { log } from 'node:console';
import { writeFile } from 'node:fs';
import path from 'node:path';
import { promisify } from 'node:util';

const writeFileAsync = promisify(writeFile);

/**
 * @param {Object} params
 * @param {String} params.pageName
 */
const genPageEntry = async ({ pageName }) => {
  // 入口文件定义
  // TODO 适当修改调整
  const entryJs = `
import '../../assets/main.css'

import { createApp } from 'vue'
// import { createPinia } from 'pinia'

import App from './${pageName}.vue'
// import router from './router'

const app = createApp(App)

// app.use(createPinia())

app.mount('#fasPage')

  `

  const fileName = path.resolve(__dirname, `./${pageName}/pageEntry.js`);

  try {
    await writeFileAsync(fileName, entryJs, { encoding: 'utf-8' });

    console.log('---------');
    console.log("按页面生成入口文件", fileName);
  } catch (error) {
    log(error);
    throw new Error(error)
  }
};

export default genPageEntry;
